using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Xml;
using InfoJetSoft.Service.Entity;

namespace InfoJetSoft.Service.Util
{
	/// <summary>
	/// Summary description for DataObjectDBUtil.
	/// </summary>
	public class DBDataTableFiller
	{

		public static DataTable fillDataTable( DataSet dataSet, XmlDocument xmlDoc, string tableName )
		{
			XmlNamespaceManager nsManager = new XmlNamespaceManager( xmlDoc.NameTable ); 
			nsManager.AddNamespace("d", JetConstant.URI_D);
			XmlNodeList recordElementList = DOMUtil.selectNodes( xmlDoc.DocumentElement, "//d:" + tableName, 0, nsManager );
			
			DataTable dataTable = dataSet.Tables[ tableName ];
			if( dataTable != null )
			{
				foreach( XmlElement recordElement in recordElementList )
				{
					DataRow dataRow = dataTable.NewRow();
					foreach( DataColumn column in dataTable.Columns )
					{
						string columnValue = recordElement.GetAttribute( column.ColumnName );
						if( columnValue.Length > 0 )
						{
							dataRow[ column.ColumnName ] = columnValue;
						}
					}
					dataTable.Rows.Add( dataRow );
				}
			}
			return dataTable;
		}

		public static bool isAllNewRecords( XmlDocument xmlDoc )
		{
			string[] dataFieldsName = new string[]{ "dataFields" };
			XmlElement dataFieldsElement = DOMUtil.findElementByLocalNames( xmlDoc, dataFieldsName );
			XmlNamespaceManager nsManager = new XmlNamespaceManager( xmlDoc.NameTable ); 
			nsManager.AddNamespace("xdado", JetConstant.URI_XDADO);
			XmlNodeList jetRecOrderAttrList = DOMUtil.selectNodes( dataFieldsElement, "//@xdado:" + JetConstant.InfoJetADORecOrderLocalName, 0, nsManager );
			if( jetRecOrderAttrList.Count <= 0 )
			{
				return true;
			}
			else
			{
				return false;
			}
		}

	}
}
